#include "types.h"
#include "sbox.h"
#include "field.h"

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main()
{
	byte before_sbox[4] = {
		0
	};
	byte after_sbox[4] = {
		0
	};

	byte coeff[4] = {
		0x0e, 0x09, 0x0d, 0x0b
	};

	byte e = 0x3f;
	int sample_count = 0;

	int ins_count = 0;
	byte* ins_list = (byte*) malloc(0x100);

	srand(time(NULL));

	byte new_byte = 0x00;
	while(1)
	{
		++sample_count;

		byte tmp = sbox[new_byte];
		
		int i,j;
		for( i = 0; i < 4; ++i)
		{
			before_sbox[i] = tmp ^ gf_256_11b_mul(e, coeff[i]);
			after_sbox[i] = inv_sbox[before_sbox[i]];

			int found = 0;
			for(j = 0; j < ins_count; ++j)
			{
				if(ins_list[j] == after_sbox[i])
				{
					found = 1;
					break;
				}
			}
			if(!found)
			{
				ins_list[++ins_count] = after_sbox[i];
			}
		}

		if(ins_count == 256)
			break;
		
		while(1)
		{
			i = rand() % 256;
			int found = 0;
			for(j = 0; j < ins_count; ++j)
			{
				if(ins_list[j] == i)
				{
					found = 1;
					break;
				}
			}
			if(!found)
				break;
		}
		new_byte = i;

		e = (e+1) % 256;

		printf("sample count:%d, ins count:%d\n", sample_count, ins_count);

	}

	printf("sample count:%d\n", sample_count);

	return 0;
}
